import axios from "axios";
// import { MessageBox } from "element-ui";
import { operationCtx } from "./auth";

// 南京环境：测试
// window.baseUrl = process.env.NODE_ENV === "development" ? "http://10.135.56.21:8011/backend/" : "http://10.135.56.21:8011/backend/";
// window.fileLoadUrl = process.env.NODE_ENV === "development" ? "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile" : "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile";

// 上海环境：生产
 window.baseUrl = process.env.NODE_ENV === "development" ? "http://localhost:8011/backend/" : "http://localhost:8011/backend/";
 window.fileLoadUrl = process.env.NODE_ENV === "development" ? "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile" : "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile";

let http = axios.create({
	baseURL: window.baseUrl,
	timeout: 60000 * 5
})

http.interceptors.request.use(config => {
	// console.log(config);
	let data = config.data;
	config.data = {
		req: data,
		operationCtx
	}
	return config;
}, err => {
	return Promise.reject(err);
});

http.interceptors.response.use(result => {
	// console.log(result);
	let {data} = result;
	let {success} = data;
	if (!success) {
		// let { errorCode, errorMessage } = data;
		// MessageBox.alert(errorMessage, "错误码：" + errorCode, {
		//     type: "error",
		//     confirmButtonText: "确定"
		// });
		return null;
	} else {
		return data;
	}
	// return data;
}, err => {
	let errMess = "", errCode = "";
	if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
		errCode = "timeout";
		errMess = "请求超时，后台服务连接失败";
	}
	if (err.response) {
		if (err.response.status === 504) {
			errCode = 504;
			errMess = "服务器走丢了，网关超时";
		} else if (err.response.status === 500) {
			errCode = 500;
			errMess = "服务器内部服务错误";
		} else if (err.response.status === 400) {
			errCode = 400;
			errMess = "请求参数错误";
		} else if (err.response.status === 401) {
			errCode = 401;
			errMess = "登录信息失效";
		} else if (err.response.status === 404) {
			errCode = 404;
			errMess = "资源或接口丢失";
		}
	}
	console.log(errMess);
	console.log(errCode);
	// MessageBox.alert(errMess, "错误码：" + errCode, {
	//     type: "error",
	//     confirmButtonText: "确定"
	// });
	return Promise.reject(err);
});

export default http;
